package com.itlong.cloud.smartcard.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.DeviceBusinessChangeLogPO;
import com.itlong.cloud.POJO.PO.DeviceBusinessElevatorChangeLogPO;
import com.itlong.cloud.POJO.PO.DeviceBusinessEntranceChangeLogPO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.enumerate.CloudElevatorChangeBusinessEnum;
import com.itlong.cloud.enumerate.CloudEntranceChangeBusinessEnum;
import com.itlong.cloud.enumerate.SmartCommunityEnum;
import com.itlong.cloud.rabbitmq.config.MessageSender;
import com.itlong.cloud.smartcard.dao.ISmartCardPropertyValueDao;
import com.itlong.cloud.smartcard.service.ISmartCardPropertyValueService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      一卡通物业增值业务处理实现类
 * </desc>
 *
 * @createDate 2019/06/12
 */
@Service
public class SmartCardPropertyValuseServiceImpl implements ISmartCardPropertyValueService{
    @Autowired
    ISmartCardPropertyValueDao iSmartCardPropertyValueDao;

    @Autowired
    MessageSender messageSender;

    /**
     * <desc>
     *      获取项目下的楼栋单元房间信息
     * </desc>
     *
     * @param json
     * @return
     * @author Jiaqi.X
     * @createDate 2019/06/12
     */
    @Override
    public Map<String, Object> getBuildAndUnitAndRoomInfo(String json) throws IOException {
        Map<String,Object> params = JsonUtil.toObject(json, HashMap.class);
        String projectId = params.get("projectId").toString();
        Map<String,Object> result = new HashMap<>();
        List<Map<String,Object>> builds = iSmartCardPropertyValueDao.getBuildInfos(projectId);
        List<Map<String,Object>> units = iSmartCardPropertyValueDao.getUnitInfos(projectId);
        Integer currentPage = Integer.valueOf(params.get("currentPage").toString());
        result.put("currentPage",currentPage);
        currentPage = (currentPage - 1)*500;
        Integer pageSize = 500;
        List<Map<String,Object>> rooms = iSmartCardPropertyValueDao.getRoomInfos(projectId,currentPage,pageSize);
        Integer total = iSmartCardPropertyValueDao.getRoomInfosCount(projectId);
        result.put("builds",builds);
        result.put("units",units);
        result.put("rooms",rooms);
        result.put("total",(total+pageSize-1)/pageSize);
        return result;
    }


    /**
     * <desc>
     *      保存软卡号
     * </desc>
     *
     * @param json
     * @return
     * @author Jiaqi.X
     * @createDate 2019/06/13
     */
    @Override
    public Integer saveSoftCardNo(String json) throws Exception {
        Map<String,Object> rooms = JsonUtil.toObject(json,HashMap.class);
        Integer result = 0;
        JSONArray pushRoom = new JSONArray();
        List<DeviceBusinessElevatorChangeLogPO> elevatorChanges = new ArrayList<>();
        for(String roomId : rooms.keySet()){
            if(!roomId.equals("projectId")) {
                JSONObject j = new JSONObject();
                j.put("roomId", roomId);
                pushRoom.add(j);
                Map<String,Object> info = (HashMap)rooms.get(roomId);
                result = iSmartCardPropertyValueDao.saveSoftCardNo(roomId, info.get("softCardNo").toString(),
                        StringHandlerUtil.getNullOrStringForObject(info.get("hardCardNo")),StringHandlerUtil.getNullOrStringForObject(info.get("fingerNo")));
                Map<String,Object> blackList = new HashMap<>();
                blackList.put("projectId", rooms.get("projectId").toString());
                blackList.put("roomId", roomId);
                blackList.put("version", 0);
                DeviceBusinessElevatorChangeLogPO elevatorPo = new DeviceBusinessElevatorChangeLogPO(rooms.get("projectId").toString(), CloudElevatorChangeBusinessEnum.PUSH_BLACK_LIST, JsonUtil.toJSON(blackList));
                elevatorChanges.add(elevatorPo);
            }
        }
        //云对讲
        List<DeviceBusinessChangeLogPO> changeLogPOS = new ArrayList<>();
        DeviceBusinessChangeLogPO deviceBusinessChangeLogPO =
                new DeviceBusinessChangeLogPO(SmartCommunityEnum.PUSH_ROOM, pushRoom.toJSONString(), rooms.get("projectId").toString());
        changeLogPOS.add(deviceBusinessChangeLogPO);
        JSONObject datajs = new JSONObject();
        datajs.put("data", changeLogPOS);
        messageSender.send(RabbitMQConstant.RABBITMQ_HANDLE_SMART_COMMUNITY_ORDERS_QUEUE,
                JsonUtil.toJSON(datajs), RabbitMQConstant.RABBITMQ_SMART_COMMUNITY_ORDERS_EXCHANGE,
                RabbitMQConstant.RABBITMQ_SMART_COMMUNITY_ORDERS_ROUTING_KEY);
        //云电梯
        DeviceBusinessElevatorChangeLogPO deviceBusinessElevatorChangeLogPO
                = new DeviceBusinessElevatorChangeLogPO(rooms.get("projectId").toString(),
                CloudElevatorChangeBusinessEnum.PUSH_ROOM_PWD,pushRoom.toJSONString());
        List<DeviceBusinessElevatorChangeLogPO> deviceElevatorChangeLogPOS = new ArrayList<>();
        deviceElevatorChangeLogPOS.add(deviceBusinessElevatorChangeLogPO);
        datajs = new JSONObject();
        datajs.put("data", deviceElevatorChangeLogPOS);
        messageSender.send(
                RabbitMQConstant.RABBITMQ_HANDLE_CLOUD_ELEVATOR_CHANGE_BUSINESS_QUEUE,
                JsonUtil.toJSON(datajs),
                RabbitMQConstant.RABBITMQ_CLOUD_ELEVATOR_CHANGE_BUSINESS_EXCHANGE,
                RabbitMQConstant.RABBITMQ_CLOUD_ELEVATOR_CHANGE_BUSINESS_ROUTING_KEY);
        List<DeviceBusinessEntranceChangeLogPO> entranceChanges = new ArrayList<>();
        DeviceBusinessEntranceChangeLogPO entrancePO = new DeviceBusinessEntranceChangeLogPO(rooms.get("projectId").toString(), CloudEntranceChangeBusinessEnum.PUSH_ROOM_PWD, JsonUtil.toJSON(pushRoom));
        entranceChanges.add(entrancePO);
        // 发送云门禁指令
        JSONObject dataJs = new JSONObject();
        dataJs.put("data",entranceChanges);
        messageSender.send(
                RabbitMQConstant.RABBITMQ_HANDLE_CLOUD_ENTRANCE_CHANGE_BUSINESS_QUEUE,
                JSON.toJSONString(dataJs),
                RabbitMQConstant.RABBITMQ_CLOUD_ENTRANCE_CHANGE_BUSINESS_EXCHANGE,
                RabbitMQConstant.RABBITMQ_CLOUD_ENTRANCE_CHANGE_BUSINESS_ROUTING_KEY
        );
        JSONObject pushJs = new JSONObject();
        pushJs.put("data", elevatorChanges);
        //推送云电梯业务指令
        messageSender.send(
                RabbitMQConstant.RABBITMQ_HANDLE_CLOUD_ELEVATOR_CHANGE_BUSINESS_QUEUE,
                JsonUtil.toJSON(pushJs),
                RabbitMQConstant.RABBITMQ_CLOUD_ELEVATOR_CHANGE_BUSINESS_EXCHANGE,
                RabbitMQConstant.RABBITMQ_CLOUD_ELEVATOR_CHANGE_BUSINESS_ROUTING_KEY);
        return result;
    }
}
